package com.qububu.play.dineTogether.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;

import org.springframework.stereotype.Service;

import com.qububu.db.dao.DineFoodDao;
import com.qububu.play.dineTogether.dto.ActionFoodDto;
import com.qububu.play.dineTogether.dto.ActionFoodDto.StapleFood;
import com.qububu.play.dineTogether.service.DineFoodService;
import com.qububu.utils.ParamConverter;
import com.qububu.utils.SystemUtil;

@Service
public class DineFoodServiceImpl implements DineFoodService{
	
	@Resource
	private DineFoodDao dineFoodDao;
	
	@Resource
	private Cache dineFoodCache;// 根据actionId存入foodList列表, 目前只考虑存入正在进行的活动的foodList(只有一个), 缺少缓存大小的监听及, 不使用将foodList分解成一个一个的food存入缓存,是因为后期查看过期活动的时候可能会出现缓存中加入多个活动的foodList
	
	public boolean doAddActionFood(ActionFoodDto actionFood) {
		/*int key = ParamConverter.convertToInteger(SystemUtil.decrypt(actionFood.getActionId()));
		if (removeElementFromCache(key)) {
			dineFoodDao.doAddActionFood(actionFood);
			
			return true;
		}
		return false;*/
		
		dineFoodDao.doAddActionFood(actionFood);
		return true;
	}
	
	public List<ActionFoodDto> getActionFoodListByActionId(int actionId) {
		/*List<ActionFoodDto> foodList = (List<ActionFoodDto>) getElementFromCache(actionId);
		if (foodList == null) {
			foodList = dineFoodDao.getActionFoodListByActionId(actionId);
			doSaveElemToCache(actionId, foodList);
		}*/
		List<ActionFoodDto> foodList = dineFoodDao.getActionFoodListByActionId(actionId);
		return foodList;
	}

	public ActionFoodDto getActionFoodInfoByFoodId(int foodIdInt) {
		ActionFoodDto actionFoodDto = dineFoodDao.getActionFoodInfoByFoodId(foodIdInt);
		return actionFoodDto;
	}

	public boolean doModifyActionFood(ActionFoodDto actionFood) {
		/*int key = ParamConverter.convertToInteger(SystemUtil.decrypt(actionFood.getActionId()));
		if (removeElementFromCache(key)) {
			dineFoodDao.doModifyActionFood(actionFood);
			
			return true;
		}
		
		return false;*/
		
		dineFoodDao.doModifyActionFood(actionFood);
		return true;
	}

	public boolean doDeleteActionFoodByFoodId(int foodId) {
		// 清空缓存中所有数据
		/*if (clearOfCache()) {
			return dineFoodDao.doDeleteActionFoodByFoodId(foodId);
		}
		
		return false;*/
		
		return dineFoodDao.doDeleteActionFoodByFoodId(foodId);
	}
	
	private Object getElementFromCache(int key) {
		Element cacheElem = dineFoodCache.get(key);

		if (cacheElem != null) {
//			System.out.println("\n\n--------调试--[dineFood]--[从缓存中[取出]一个元素]-------\n\n");
			return cacheElem.getObjectValue();
		}
		return null;
	}
	
	private void doSaveElemToCache(int key, Object elem) {
		Element foodElement = dineFoodCache.get(key);
		if (foodElement == null) {
			dineFoodCache.put(new Element(key, elem));
//			System.out.println("\n\n--------调试--[dineFood]--[从缓存中[存入]一个元素]-------\n\n");
		}
	}

	// 清除指定key的缓存数据
	private boolean removeElementFromCache(int key) {
		if (dineFoodCache.get(key) != null) {
//			System.out.println("\n\n--------调试--[dineFood]--[从缓存中[移除]一个元素]-------\n\n");
			return dineFoodCache.remove(key);	
		}
		return true;
	}
	
	/**
	 * 清空缓存中所有数据
	 * */
	private boolean clearOfCache() {
		if (dineFoodCache.getSize() > 0) {
			dineFoodCache.removeAll();
//			System.out.println("\n\n--------调试--[dineFood]--[清空缓存数据]-------\n\n");
		}
		return true;
	}	
}
